package com.carworld.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.carworld.config.BaseController;
import com.carworld.model.SysUser;
import com.carworld.model.UserAdvice;
import com.carworld.utils.DateUtils;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.TemplateData;
import com.jfinal.weixin.sdk.api.TemplateMsgApi;

import javax.xml.bind.annotation.XmlValue;

public class AdviceController extends BaseController {
	private synchronized  Date getDate(){
		return new Date();
	}
	/*
	 * 保存用户建议与反 馈
	 */
	public void saveAdvice() throws UnsupportedEncodingException, ParseException {
		String content=getPara("content");
		String queryCon= URLDecoder.decode(content,"utf-8");
		UserAdvice advice = new UserAdvice();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String format = sf.format(new Date());
		Date parse = sf.parse(format);
		advice.setId(getDate());//类型不匹配
		advice.setUserId(getUserCookie());
		advice.setAdvice(queryCon);
		advice.setUserTelephone(getPara("telephone"));
		advice.setCreateTime(parse);
        advice.setCompanyId(SysUser.dao.findById(getUserCookie()).getCompanyId());
		advice.save();
		sendAdvice(getUserCookie(),advice);
		renderText("留言成功，我们会尽快处理的~");
	}

	//发送意见反馈通知
	public void sendAdvice(String userid,UserAdvice advice){
			SysUser uu=SysUser.dao.findById(userid);
			ApiResult apiResult = TemplateMsgApi.send(TemplateData.New().setTouser("ocgWcws9Z8X-BV1bAOTTj0ExM_oo")
					.setTemplate_id("khoI26gkuF4GAvlnMZNtqrozkEymgQpWZjfn9p2wk7E") // 意见反馈通知模板id
					.add("first", "新的意见反馈消息提示：\n")
					.add("keyword1",   DateUtils.getCurrentTime(new Date())+"的一条意见", "#173177")
					.add("keyword2",uu.getNickname()+"\n", "#173177")
					.add("keyword3",  advice.getAdvice()+ "\n", "#173177")
					.add("remark", "", "#173177").build());
			System.out.println("通知店长：意见的消息： " + apiResult.getJson());
	}

}
